package cn.edu.scau.cmi.front.service;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;

import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.Metamodel;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.engine.internal.Collections;
import org.hibernate.hql.internal.ast.QueryTranslatorImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;

import cn.edu.scau.cmi.ema.domain.Address;
import cn.edu.scau.cmi.ema.domain.Event;
import cn.edu.scau.cmi.ema.domain.Feature;
import cn.edu.scau.cmi.ema.domain.Food;
import cn.edu.scau.cmi.ema.domain.Foodbatch;
import cn.edu.scau.cmi.ema.domain.Picture;
import cn.edu.scau.cmi.ema.domain.Standard;
import cn.edu.scau.cmi.ema.domain.Subject;
import cn.edu.scau.cmi.ema.domain.User;
import cn.edu.scau.cmi.ema.domain.Video;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;
import cn.edu.scau.cmi.ema.service.base.CmiEmaService;
import cn.edu.scau.cmi.ema.service.base.CmiPsonService;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import cn.edu.scau.cmi.front.dao.CmiAddressDAO;
import cn.edu.scau.cmi.front.dao.CmiEventDAO;
import cn.edu.scau.cmi.front.dao.CmiKnowledgeBaseDAO;
import cn.edu.scau.cmi.front.domain.AddressWrap;

/**
 * Spring service that handles CRUD requests for FrontService
 * 
 */

@Service("MobileService")

@Transactional
public class MobileServiceImpl extends CmiEmaService implements MobileService {

	@Autowired
	CmiPsonService cmiPsonService;

	@Autowired
	CmiAddressDAO cmiAddressDAO;

	@Autowired
	CmiEventDAO cmiEventDAO;

	public MobileServiceImpl() {
	}
	
//	TODO 2021-1-14 获取所有的实体的json对象，到Service中去。
//	（1）需要精确的权限控制，（2）需要排序
	public JSONObject getEntitiesJsonObject() {
		JSONObject entitiesJsonObject = new JSONObject();
		
		Metamodel metamodel = entityManager.getMetamodel();
		Set<EntityType<?>> entities = metamodel.getEntities();
		for(EntityType<?> entity : entities) {
			JSONObject authrotyJsonObject = new JSONObject();
			String entityName = entity.getJavaType().getName();
//			cminamedquery不是实体，是一个仅仅只有命名属性的实体，需要删除这个实体，以免客户端缺陷
			if("cminamedquery".equalsIgnoreCase(entityName)) {
				continue;
			}
			
			authrotyJsonObject.put("name", entityName);
			authrotyJsonObject.put("new", true);
	    	authrotyJsonObject.put("delete", true);
	    	authrotyJsonObject.put("edit", true);
	    	authrotyJsonObject.put("list", true);
	    	authrotyJsonObject.put("search", true);
	    	
	    	entitiesJsonObject.put(entity.getName().toLowerCase(), authrotyJsonObject);
		}

	    return entitiesJsonObject;
	}
}